﻿CREATE VIEW dbo.vwActiveTakeoutProviders
AS
SELECT     TOP 100 PERCENT dbo.AudienceMembers.UserID, dbo.Audience.Name AS Audience, dbo.TakeoutProvider.Id AS TakeoutProviderID, 
                      dbo.Menus.Name AS MenuName, dbo.PaymentSource.Name AS PaymentSourceName, dbo.TakeoutProviderAudiences.AudienceId, 
                      dbo.TakeoutProvider.PaymentSourceID, dbo.TakeoutProvider.ExpirationTime, dbo.TakeoutProvider.TakeoutCharge, dbo.TakeoutProvider.Instructions, 
                      dbo.TakeoutProvider.AllowPayMeLater, dbo.Category.Name AS CategoryName, dbo.Menus.CategoryId, dbo.TakeoutProvider.MenuID, 
                      dbo.TakeoutProvider.MaximumOrders, dbo.Audience.[SiteID]
FROM         dbo.TakeoutProvider INNER JOIN
                      dbo.TakeoutProviderAudiences ON dbo.TakeoutProvider.Id = dbo.TakeoutProviderAudiences.TakeoutProviderId INNER JOIN
                      dbo.Audience ON dbo.TakeoutProviderAudiences.AudienceId = dbo.Audience.Id INNER JOIN
                      dbo.AudienceMembers ON dbo.Audience.Id = dbo.AudienceMembers.AudienceId INNER JOIN
                      dbo.Menus ON dbo.TakeoutProvider.MenuID = dbo.Menus.Id INNER JOIN
                      dbo.PaymentSource ON dbo.TakeoutProvider.PaymentSourceID = dbo.PaymentSource.Id INNER JOIN
                      dbo.Category ON dbo.Menus.CategoryId = dbo.Category.Id
WHERE     (dbo.TakeoutProvider.ExpirationTime > GETDATE()) AND (dbo.TakeoutProvider.StartTime < GETDATE()) AND (dbo.TakeoutProvider.MaximumOrders > 0)
ORDER BY dbo.TakeoutProviderAudiences.AudienceId


